package com.utoronto.syncgallery.utils;

import java.io.File;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.text.Editable;
import android.widget.EditText;

import com.utoronto.syncgallery.GalleryActivity;
import com.utoronto.syncgallery.IF.GalleryBasicOperations;

/**
 * Define all the basic operations for the gallery
 * 
 * @author KOBE, DANIEL, JACKY
 * 
 */

public class GalleryOperations implements GalleryBasicOperations {

	private GalleryActivity gallery;
	private File target;
	private Context context;
	private int type;

	public GalleryOperations(GalleryActivity gallery, File file, Context c,
			int type) {
		this.gallery = gallery;
		this.target = file;
		this.context = c;
		this.type = type;
	}

	/** Delete selected picture */
	public void delete() {

		/**
		 * When the dropbox is authenticated and the user is in the SyncGallery
		 * folder.
		 */

		if (type == 1) {
			ImageManagement.delete(gallery, target);
			deleteMetaFile();
		} else {
			FileManagement.delete(gallery, target);
		}
	}

	/** Rename selected picture */
	public void rename() {
		final EditText input = new EditText(gallery);
		final AlertDialog.Builder builder = new AlertDialog.Builder(gallery);

		/** if the user choose to rename the file with given name */
		final DialogInterface.OnClickListener listener1 = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				/** Get new file name. */
				Editable newname = input.getText();

				/** Rename both the selected file and its thumbnail. */
				if (type == 1) {
					if (ImageManagement.rename(gallery, target,
							newname.toString())) {
						ImageManagement.rename_thumbnail(gallery, target,
								newname.toString());
						;
					}

					deleteMetaFile();
				} else
					FileManagement.rename(gallery, target, newname.toString());

				/** Refresh the current directory. */
				gallery.browseTo(target.getParentFile());
			}
		};

		/** Do nothing if the user cancel the operation */
		final DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		};

		/** Build the rename UI interface. */
		builder.setTitle("Rename");
		builder.setView(input);
		builder.setPositiveButton("Ok", listener1);
		builder.setNegativeButton("Cancel", listener2);
		builder.show();
	}

	/** Move the selected picture to SyncGallery folder. */
	public void move() {
		final File parent = target.getParentFile();
		ImageManagement.move_to_gallery(gallery, target);
		gallery.browseTo(parent);
	}

	/** Share the selected picture. */
	public void share() {
		/** Parse the intent to the Android default share menu. */
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setType("image/*");
		intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(target));
		gallery.startActivity(Intent.createChooser(intent, null));
	}

	public void create_folder() {
		final EditText input = new EditText(gallery);
		final AlertDialog.Builder builder = new AlertDialog.Builder(gallery);

		/** if the user choose to rename the file with given name */
		final DialogInterface.OnClickListener listener1 = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {

				/** Get new file name. */
				Editable newname = input.getText();

				/** Rename both the selected file and its thumbnail. */
				FileManagement.create_folder(gallery, target,
						newname.toString());
				gallery.browseTo(target);
			}
		};

		/** Do nothing if the user cancel the operation */
		final DialogInterface.OnClickListener listener2 = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}
		};

		/** Build the rename UI interface. */
		builder.setTitle("Create a new folder");
		builder.setView(input);
		builder.setPositiveButton("Ok", listener1);
		builder.setNegativeButton("Cancel", listener2);
		builder.show();
	}

	public void show_properties() {
		if (type == 0) {
			FileManagement.show_properties(gallery, target);
		}
		if (type == 1) {
			ImageManagement.show_properties(gallery, target);
		}
	}

	private void deleteMetaFile() {
		if (target.getAbsolutePath().startsWith(
				SyncGalleryConstants.Gallery_SYNC_DIR)) {
			final String filename = target.getAbsolutePath().split(
					SyncGalleryConstants.Gallery_SYNC_DIR)[1];
			context.deleteFile(filename.substring(1).replace("/", "-")
					+ ".meta");
		}
	}

	/** Return the OnClickListener for Folder */
	public final OnClickListener getFolderListener() {
		return new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				// Delete selected folder.
				case 0:
					delete();
					break;
				// Rename a selected folder.
				case 1:
					rename();
					break;
				// Show the properties of the selected folders.
				case 2:
					show_properties();
					break;
				}
				dialog.dismiss();
			}
		};
	}

	/** Return the OnClickListener for Files in SyncGallery Folder */
	public final OnClickListener getSyncFileListener() {
		return new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				// Delete selected file.
				case 0:
					delete();
					break;
				// Share file through other activity.
				case 1:
					share();
					break;
				// Rename a selected file.
				case 2:
					rename();
					break;
				// Show properties of the selected files.
				case 3:
					show_properties();
					break;
				}
				dialog.dismiss();
			}
		};
	}

	/** Return the OnClickListener for Local files */
	public final OnClickListener getLocalFileListener() {
		return new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				switch (which) {
				// Delete selected file.
				case 0:
					delete();
					break;
				// Share file through other activity.
				case 1:
					share();
					break;
				// Move selected file to SyncGallery folder.
				case 2:
					move();
					break;
				// Rename a selected file.
				case 3:
					rename();
					break;
				// Show properties of the selected files.
				case 4:
					show_properties();
					break;
				}
				dialog.dismiss();
			}
		};
	}
}
